home *** CD-ROM | disk | FTP | other *** search
/ CD-ROM Today - The Disc! 5 / CD-ROM Today - The Disc (Issue 5)(November 1994).ISO / mac / Mac shareware / Education / RLaB / rlib / intersection.r < prev    next >
Text File  |  1994-09-21  |  803b  |  43 lines

  1. //-------------------------------------------------------------------//
  2. //  intersection
  3.  
  4. //  Syntax:    intersection ( A , B )
  5.  
  6. //  Description:
  7.  
  8. //  Intersection finds the intersection-set of the two vector sets A,
  9. //  and B.
  10.  
  11. //  See Also: complement, set, union
  12. //-------------------------------------------------------------------//
  13.  
  14. intersection = function ( A , B )
  15. {
  16.   local (Int, a, b, i, j, tmp)
  17.  
  18.   if(A.nr == 0 || B.nr == 0) {
  19.     return [];
  20.   }
  21.  
  22.   if (min (size (A)) != 1) {
  23.     error ("intersection: 1st arg must be a vector");
  24.   }
  25.  
  26.   if (min (size (B)) != 1) {
  27.     error ("intersection: 2st arg must be a vector");
  28.   }
  29.  
  30.   a = set (A); b = set (B);
  31.   j = 1;
  32.   for (i in 1:b.n)
  33.   {
  34.     tmp = find (b[i] == a);
  35.     if (tmp.n > 0) 
  36.     {
  37.       Int[j] = b[i];
  38.       j++;
  39.     }
  40.   }
  41.   return Int
  42. };
  43.